import axios from 'axios'

async function webGetSkuInfo(sku) {

  const account=window.localStorage.getItem('account')?window.localStorage.getItem('account'):"lbl"
  console.log('account',account)
  const response = await axios.post('https://api.xshop.fun/query', {
    account: account,
    value: sku,
    field: 'sku'
  })

  return response.data.results[0]
}

async function webSaveSkuInfo(sku, skuInfo) {
  let webSkuInfo = await webGetSkuInfo(sku)
  if(webSkuInfo){
    skuInfo = Object.assign(JSON.parse(webSkuInfo.detail), skuInfo)
  }
  const account=window.localStorage.getItem('account')?window.localStorage.getItem('account'):"lbl"
  const response = await axios.post('https://api.xshop.fun/update', {
    account: account,
    sku: sku,
    asin: ' ',
    detail: JSON.stringify(skuInfo)
  })
  return(response.data.success)
}

function findCol(sheet, cloName)
{
  let col = 1
  do {
    if (sheet.Cells.Item(3, col).Value2.includes(cloName)) {
      return col
    }
    col++
  } while (sheet.Cells.Item(1, col).Value2)

  return 0
}

export async function WriteSkuInfo() {
  const field = window.localStorage.getItem('field')?Number(window.localStorage.getItem('field')):3
  const start = window.localStorage.getItem('start')?Number(window.localStorage.getItem('start')):4
  const cloName = window.localStorage.getItem('cloName')?window.localStorage.getItem('cloName'):"sku"

  const activeSheet = window.wps.EtApplication().ActiveWorkbook.ActiveSheet
  const colRt = activeSheet.Cells.Item(1, activeSheet.Columns.Count).End(-4159).Column
  const skuColumn=findCol(activeSheet,cloName)
  const lanRt = activeSheet.Cells.Item(activeSheet.Rows.Count, skuColumn).End(-4162).Row

  for (let i = start; i <= lanRt; i++) {
    const value = activeSheet.Cells.Item(i, skuColumn).Value2
    if (!value) { continue }
    const skuInfo=await webGetSkuInfo(value)
    if(!skuInfo){ continue}
    const data=JSON.parse(skuInfo.detail)
    for (let index = 1; (index < colRt) && (Object.keys(data).length > 0); index++) {

      if ((!activeSheet.Cells.Item(i, index).Value2) && (activeSheet.Cells.Item(field, index).Value2 in data)) {
        activeSheet.Cells.Item(i, index).Value2 = data[activeSheet.Cells.Item(field, index).Value2]

      }

    }
  }
  alert('读取完毕','xinfei')
}

export async function SaveSkuInfo(message) {

  const field = window.localStorage.getItem('field')?Number(window.localStorage.getItem('field')):3
  const start = window.localStorage.getItem('start')?Number(window.localStorage.getItem('start')):4
  const cloName = window.localStorage.getItem('cloName')?window.localStorage.getItem('cloName'):"sku"
  const activeSheet = window.wps.EtApplication().ActiveWorkbook.ActiveSheet
  const colRt = activeSheet.Cells.Item(1, activeSheet.Columns.Count).End(-4159).Column
  const skuColumn = findCol(activeSheet, cloName)
  console.log(skuColumn)
  const lanRt = activeSheet.Cells.Item(activeSheet.Rows.Count, skuColumn).End(-4162).Row
  if (skuColumn === 0) {
    alert('找不到SKU列', 'xinfei')
    return
  }
  for (let i = start; i <= lanRt; i++) {
    const sku = activeSheet.Cells.Item(i, skuColumn).Value2
    console.log(sku, i, skuColumn)
    if (!sku) {
      break
    }
    let data = {}
    for (let c = 1; c <= colRt; c++) {
      const item = activeSheet.Cells.Item(field, c).Value2
      const value = activeSheet.Cells.Item(i, c).Value2
      if (value) {
        data[item] = value
      }
    }
    await webSaveSkuInfo(sku, data)
  }
  alert('保存完毕', 'xinfei')
}
